<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="充电桩编号" prop="equipNo">
        <el-input
          v-model="queryParams.equipNo"
          placeholder="请输入充电桩编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="插座号" prop="channelNo">
        <el-input
          v-model="queryParams.channelNo"
          placeholder="请输入插座号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入电卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电状态" prop="tranType">
        <el-select v-model="queryParams.chargingStatus" placeholder="请选充电状态" clearable>
          <el-option
            v-for="dict in dict.type.charging_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="充电日期" prop="createTime">
        <el-date-picker clearable
            v-model="queryParams.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="充电记录id" align="center" prop="chargingRecordId" />-->
<!--      <el-table-column label="充电桩id" align="center" prop="equipId" />-->
      <el-table-column label="充电桩" align="center" prop="equipNo" />
      <el-table-column label="插座号" align="center" prop="channelNo" />
      <el-table-column label="用户/电卡号" align="center" prop="userName" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.userName">{{scope.row.userName}}</div>
          <div v-else>{{scope.row.cardNo}}</div>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="cardNo" >
        <template slot-scope="scope">
          <div v-if="scope.row.userName">用户充电</div>
          <div v-else>电卡充电</div>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="equipType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equip_type" :value="scope.row.equipType"/>
        </template>
      </el-table-column>
      <el-table-column label="充电方案" align="center" prop="chargingSchemeId" width="120" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dict_charging_scheme" :value="scope.row.chargingSchemeId"/>
        </template>
      </el-table-column>
      <el-table-column label="充电状态" align="center" prop="chargingStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.charging_status" :value="scope.row.chargingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="消费金额" align="center" prop="amount" />
      <el-table-column label="用电度数" align="center" prop="degree" />
      <el-table-column label="起充价格" align="center" prop="startPrice" />
      <el-table-column label="流水号" align="center" prop="orderNo" />
      <el-table-column label="计费次数" align="center" prop="numTimes" />
<!--      <el-table-column label="功率请求失败次数" align="center" prop="errorTimes" />-->
      <el-table-column label="结束类型" align="center" prop="endType" />
      <el-table-column label="计划充电时长" align="center" prop="planChargingTime" width="120"/>
      <el-table-column label="充电功率" align="center" prop="chargingPower" />
      <el-table-column label="最大充电功率" align="center" prop="maxChargingPower" width="120" />

      <el-table-column label="充满不自停" align="center" prop="chargingFullNotStop" width="120"/>
      <el-table-column label="电度数" align="center" prop="remark" />
      <el-table-column label="开始充电时间" align="center" prop="createTime" width="120"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电记录对话框 -->
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record";

export default {
  name: "Record",
  dicts: ['dict_charging_scheme','charging_status','equip_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipNo: null,
        channelNo: null,
        userId: null,
        cardId: null,
        equipType: null,
        endType: null,
        chargingStatus: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充电记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chargingRecordId: null,
        equipId: null,
        equipNo: null,
        channelNo: null,
        userId: null,
        cardId: null,
        equipType: null,
        chargingSchemeId: null,
        amount: null,
        degree: null,
        startPrice: null,
        orderNo: null,
        numTimes: null,
        errorTimes: null,
        endType: null,
        planChargingTime: null,
        chargingPower: null,
        maxChargingPower: null,
        chargingStatus: null,
        chargingFullNotStop: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chargingRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chargingRecordId = row.chargingRecordId || this.ids
      getRecord(chargingRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.chargingRecordId != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chargingRecordIds = row.chargingRecordId || this.ids;
      this.$modal.confirm('是否确认删除充电记录编号为"' + chargingRecordIds + '"的数据项？').then(function() {
        return delRecord(chargingRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
